package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.InOutStats;
import com.shchuangzhi.smartguard.core.NotifyMessage;

import org.apache.commons.cli.Parser;

import java.io.InputStream;

/**
 * Created by j_hu on 7/22/17.
 */

public class SyncInOutStatsHandler extends SyncMetaCommandRespHandler{

    public SyncInOutStatsHandler(SyncMetaCommand cmd) {
        super("inout.stats.sync", cmd);
    }

    @Override
    public boolean handle(InputStream input) {
        boolean handleOk = false;
        try {
            InOutStats stats = InOutStats.getInstance();
            stats.addDbHelper(getCommand().getDbHelper());
            InOutStats.ParseResult result = stats.parse(getString(input), false);
            if (result.isParseOk()) {
                getCommand().getDbHelper().getSyncRecord().update(getName(), result.getParseTime());
                super.getCommand().notify((NotifyMessage.info(Consts.INFO_SYNC_STATS_OK,
                        Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST)));
                getCommand().notify(NotifyMessage.wrap(1, Consts.NOTIFY_INOUT_STATS_CHANGED));
                handleOk = true;
            } else {
                super.getCommand().notify((NotifyMessage.info(Consts.INFO_SYNC_STATS_FAIL,
                        Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST)));
            }
        }catch(Exception e) {
            getCommand().raiseError(e);
        }
        return handleOk;
    }
}
